package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Userchecks;
import com.ysd.twocrm.mapper.UserChecksMapper;
import com.ysd.twocrm.service.UserCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/userchecks")
public class UserChecksController {
@Autowired
    private UserChecksMapper userChecksMapper;
@Autowired
private UserCheckService userCheckService;


    /*
     * @Description 根据用户id和用户姓名还有当天的日期（年月日）来查询，如果不存在，则返回checkDaka（前台出现打卡上班功能）
     * 如果存在，并且状态是1：则返回下班打卡 如果是2:就代表今天一天已经都打完了
     *
     * @param userId 用户id
     * @param userName 用户姓名
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/20 15:16
     */
    @GetMapping
    public Object getAllUserChecks(Integer userId,String userName){

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Userchecks> queryWrapper=new QueryWrapper<Userchecks>();
        queryWrapper.eq("userId",userId).eq("userCheckName",userName).likeRight("userCheckInTime",sdf.format(date));
        Userchecks userchecks = userChecksMapper.selectOne(queryWrapper);
        if(StringUtils.isEmpty(userchecks)){
            return "checkDaka";
        }else if(userchecks.getUserCheckStatus()==1){
            return "checkIn";
        }else if(userchecks.getUserCheckStatus()==2){
            return "checkout";
        }
        return "false";
    }
    /*
     * @Description 多条件查询分页。这里用string类型来接收了，其实按理应该是用实体类接收更加方便
     * 但是这个我们的转换方式不太一样，我要转换的是yyyy-MM-dd跟配置文件有所冲突，所以前台发来的是这个格式
     * 但是他是默认是string类型的，但是我们的实体类是util类型的，所以在这里需要进行一个转换，当然这里转换了以后
     * 还要在实现类里重新转回来，虽然有点麻烦，
     * @param page 分页页码
     * @param userCheckName 名字
     * @param userCheckInTime 打卡进入时间
     * @param userCheckOutTime 打卡出去时间
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/21 9:51
     */
    @GetMapping("/1")
    public Object getUserChecksAll(Integer page,String userCheckName,String userCheckInTime,String userCheckOutTime) throws ParseException {

           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
           Userchecks uc=new Userchecks();
          if(userCheckInTime!=null&&userCheckInTime!=""){
              uc.setUserCheckInTime(sdf.parse(userCheckInTime));
          }
     if(userCheckOutTime!=null && userCheckOutTime!=""){
         uc.setUserCheckOutTime(sdf.parse(userCheckOutTime));
     }
       if(userCheckName!=null&&userCheckName!=""){
           uc.setUserCheckName(userCheckName);
       }

        return userCheckService.USERCHECKS_LAYUI_UTILS(page,uc);
    }
    /*
     * @Description 判断刚打卡的时间是否超过10分钟
     * @param userId
     * @param userCheckName
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/20 8:18
     */
    @GetMapping("/2")
    public Object getUserChecksByTen(Integer userId,String userCheckName    ){

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Userchecks> queryWrapper=new QueryWrapper<Userchecks>();
        queryWrapper.eq("userId",userId).eq("userCheckName",userCheckName).likeRight("userCheckInTime",sdf.format(date));
        Userchecks userchecks = userChecksMapper.selectOne(queryWrapper);
        long time = userchecks.getUserCheckInTime().getTime();//打卡的时间
        long timeNow = date.getTime();//当前时间
       if(((timeNow-time)/(1000*60))>10){
           System.out.println((timeNow-time)/(1000*60));
           return "success";
       }

        return false;
    }


    /*
     * @Description 添加，状态为1 这是打卡上班的，并且添加打卡时间为当前日期
     * 打卡上班成功后去验证当天是否迟到然后将信息存到打卡记录表里
     * @param userchecks
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/20 15:46
     */
    @PostMapping
    public Object addUserCheckStatus (Userchecks userchecks){

        userchecks.setUserCheckInTime(new Date());
        int insert = userChecksMapper.insert(userchecks);
        if(insert>0){

            return "success";
        }
        return "false";
    }
    /*
     * @Description 根据当前用户名和用户id和今天的日期（年月日）来查询并修改前台传来的状态
     * 打卡下班后判断是否早退，将信息存到打卡记录表里
     * @param userchecks
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/4/20 15:45
     */
    @PutMapping
    public Object exitUserCheckStatus (Userchecks userchecks){

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Userchecks> queryWrapper=new QueryWrapper<Userchecks>();
        queryWrapper.eq("userId",userchecks.getUserId()).eq("userCheckName",userchecks.getUserCheckName()).likeRight("userCheckInTime",sdf.format(date));
        userchecks.setUserCheckOutTime(new Date());
        int insert = userChecksMapper.update(userchecks,queryWrapper);
        if(insert>0){

            return "success";
        }
        return "false";
    }
    /*
     * @Description 经理给咨询师进行签退
     * @param userchecks
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/26 9:36
     */
    @PutMapping("/1")
    public Object exitUserCheckStatusByjingi (Userchecks userchecks){

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Userchecks> queryWrapper=new QueryWrapper<Userchecks>();
        queryWrapper.eq("userCheckId",userchecks.getUserCheckId());
        userchecks.setUserCheckOutTime(new Date());
        int insert = userChecksMapper.update(userchecks,queryWrapper);
        if(insert>0){
            return "success";
        }
        return "false";
    }


}
